package com.xblibo.xruth.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xblibo.xruth.common.BaseController;
import com.xblibo.xruth.entity.User;
import com.xblibo.xruth.security.SecurityUserHolder;
import com.xblibo.xruth.service.UserService;

/**
 * <p>Title: Login.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  上午10:42:11
 */
@Controller
public class LoginController extends BaseController{
	
	@Resource
	private UserService userService;
	
	@RequestMapping(value = "/login")
	public String login(){
		return "/login";
	}
	
	@RequestMapping(value = "/main")
	public String login(String name,String password,HttpServletRequest request){
		User user  =  SecurityUserHolder.getCurrentUser();
		if(user != null){
			request.getSession().setAttribute("user", user);
			return "redirect:/shopper/index";//重定向
		}
		System.out.println(user);
		if ((name==null || StringUtils.isBlank(name))){
			return "/login";
		}else{
			User u = userService.findUniqueBy("username", name);
			System.out.println("enter login method u "+u);
			if(null != u && u.getPassword().equals(password)){
				request.getSession().setAttribute("user", u);
				return "redirect:/shopper/index";//重定向
			}else{
				return "/login";
			}
		}
	}
	
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request){
		//User user = super.getSessionUser(request);
		request.setAttribute("currentModule", "shopper");
		//request.setAttribute("functions", user.getModuleFunction().get("shopper"));
		return "/index";
	}
}
